/*
 * Created on 28.04.2003
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package cpa.prowon.gui.actions;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import org.eclipse.jface.action.Action;

import pho.prowon.category.IndexCategoryDefinition;
import cpa.prowon.gui.MainWindow;

/**
 * The select categorization action is used to switch between the different
 * categorization types which are defined via the IndexCategoryDefinition
 * objects
 * @author Chris
 */
public class SelectCategorizationAction extends Action {

	// Properties
	private static ArrayList action_instances = null;
	private IndexCategoryDefinition catDef;
	private MainWindow window;
	
	/**
	 * Create a new action of this type
	 * @param MainWindow The window where the action is used
	 * @param String The category definition name
	 * @return A new instance of this action
	 * @author Chris
	 */
	public static SelectCategorizationAction createAction(MainWindow w, IndexCategoryDefinition icd) {
		SelectCategorizationAction action = new SelectCategorizationAction(w, icd, Action.AS_CHECK_BOX);
		getActionInstances().add(action);
		return action;
	}
	
	/**
	 * Return the action instances
	 * @return The action instances
	 * @author Chris
	 */
	protected static Collection getActionInstances() {
		if (action_instances == null) {
			action_instances = new ArrayList();
		}
		return action_instances;
	}
	
	/**
	 * Get the index category definition of the action
	 * @return The index category definition used by this action
	 * @author Chris
	 */
	protected IndexCategoryDefinition getIndexCategoryDefinition() {
		return catDef;
	}
	
	/**
	 * Set the appropriate action checked by using the index category definition
	 * @param IndexCategoryDefinition The definition which is currently used
	 * @author Chris
	 */
	public static void setChecked(IndexCategoryDefinition cd) {
		Iterator i = getActionInstances().iterator();
		while (i.hasNext()) {
			SelectCategorizationAction nextAction = (SelectCategorizationAction) i.next();
			if (nextAction.getIndexCategoryDefinition().equals(cd)) {
				nextAction.setChecked(true);
			} else {
				nextAction.setChecked(false);
			}
		}
	}
	
	/**
	 * Constructor
	 * @param MainWindow The window where the action is used
	 * @param String The category definition name
	 * @author Chris
	 */
	protected SelectCategorizationAction(MainWindow w, IndexCategoryDefinition theIcd, int style) {
		
		// Call the super constructor
		super(theIcd.getName(), style);
		
		// Set the category definition
		catDef = theIcd;
		
		// Remember the application window
		window = w;
		
	}
	
	/**
	 * Return the title
	 * @return The title
	 * @author Chris
	 */
	protected String getTitle() {
		return catDef.getName();
	}

	/**
	 * Run the action
	 * @author Chris
	 */
	public void run() {
		
		// Set the tree view input
		window.getCategoryPart().setViewerInput(catDef);
		
	}

}
